package org.jgs1904.chanmufeng;

import java.lang.reflect.Constructor;

/**
 * 利用反射破坏单例
 *
 * @author: 默苍璃
 * @date: 2024-07-01 11:52
 */
public class SingletonBrokenByReflect {

    public static void main(String[] args) {

        try {
            Class<?> clazz = LazyStaticInnerClassSingleton.class;

            // 通过反射弧获取类的私有构造方法
            Constructor c = clazz.getDeclaredConstructor(null);
            // 强制访问
            c.setAccessible(true);

            Object obj1 = c.newInstance();
            Object obj2 = c.newInstance();

            // 输出false
            System.out.println(obj1 == obj2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

